﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace ThreeItems
{
    /// <summary>
    /// 负责数据在数据库中的读写
    /// </summary>
    public class DataGateWay
    {
        public static ExampleInfo Get(int id)
        {
            Database db = DatabaseFactory.CreateDatabase();
            ExampleInfo example = new ExampleInfo();
            string sqlString = "SELECT * FROM data WHERE DataCount = " + id;
            IDataReader reader = db.ExecuteReader(CommandType.Text, sqlString);
            using (reader)
            {
                if (reader.Read())
                {
                    example.Id = id;
                    example.U1 = (double)reader["u1"];
                    example.O1 = (double)reader["o1"];
                    example.E1 = (double)reader["e1"];
                    example.SD1 = (double)reader["sd1"];

                    example.U2 = (double)reader["u2"];
                    example.O2 = (double)reader["o2"];
                    example.E2 = (double)reader["e2"];
                    example.SD2 = (double)reader["sd2"];

                    example.U3 = (double)reader["u3"];
                    example.O3 = (double)reader["o3"];
                    example.E3 = (double)reader["e3"];
                    example.SD3 = (double)reader["sd3"];

                    example.R12 = (double)reader["r12"];
                    example.R13 = (double)reader["r13"];
                    example.R21 = (double)reader["r21"];
                    example.R23 = (double)reader["r23"];
                    example.R31 = (double)reader["r31"];
                    example.R32 = (double)reader["r32"];

                    //example.QC1 = (double)reader["qc1"];
                    //example.QC2 = (double)reader["qc2"];
                    //example.QC3 = (double)reader["qc3"];

                    //example.PC = (double)reader["pc"];
                }
            }
            return example;
        }

        /// <summary>
        /// 更新集中式模型的计算结果
        /// </summary>
        /// <param name="exampleInfo"></param>
        public static void UpdateCentralizedModel(ExampleInfo example)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sqlString =
                string.Format(
                    "UPDATE data SET qc1 = {0},qc2={1},qc3={2},pc1={3},pc2={4},pc3={5},pc={6},maxQc1={7},maxQc2={8},maxQc3={9} WHERE DataCount ={10} ",
                    example.QC1, example.QC2, example.QC3, example.PC1, example.PC2, example.PC3, example.PC,
                    example.MaxQC1, example.MaxQC2, example.MaxQC3, example.Id);


            db.ExecuteNonQuery(CommandType.Text, sqlString);

        }

        public static void UpdateDecentralizedModel(ExampleInfo example)
        {
            Database db = DatabaseFactory.CreateDatabase();
            string sqlString =
                string.Format(
                    "UPDATE data SET qd1 = {0},qd2={1},qd3={2},pd1={3},pd2={4},pd3={5},pd={6},prob1={7},prob2={8},prob3={9},uo1={10},uo2={11},uo3={12},maxQd1={13},maxQd2={14},maxQd3={15} WHERE DataCount ={16} ",
                    example.QD1, example.QD2, example.QD3, example.PD1, example.PD2, example.PD3, example.PD,
                    example.Prob1, example.Prob2, example.Prob3, example.UO1, example.UO2, example.UO3, example.MaxQD1,
                    example.MaxQD2, example.MaxQD3, example.Id);


            db.ExecuteNonQuery(CommandType.Text, sqlString);

        }
    }
}
